<?php

if (!isset($start)) { $start = 0; }

$total = sprintf('%.6f', microtime(true) - $start);

echo '<div style="width: 98%; clear: both; padding: 1%; font-size: 14px; margin-top: 10px; color: #fff; background-color: #000;">';

printf("Execution time: %s seconds", $total);

echo '<h1 style="margin: 10px 0">Request</h1>';
echo '<pre>';
if (Light_Core::$mainController) {
    print_r(Light_Core::$mainController->getRequest()->getParams());
}
echo '</pre>';

if (Light_Core::$db && Light_Core::$db->getProfiler() && Light_Core::$db->getProfiler()->getEnabled()) {

    $profiler     = Light_Core::$db->getProfiler();
    $totalTime    = $profiler->getTotalElapsedSecs();
    $queryCount   = $profiler->getTotalNumQueries();
    $longestTime  = 0;
    $longestQuery = null;
    $queries      = array();

    if ($profiler->getQueryProfiles()) {
        foreach ($profiler->getQueryProfiles() as $query) {
            if ($query->getElapsedSecs() > $longestTime) {
                $longestTime  = $query->getElapsedSecs();
                $longestQuery = $query->getQuery();
            }
            $queries[] = '<div style="border: 1px Lightid #f2f2f2; padding: 10px; margin: 10px 0">' . $query->getQuery() . '</div>';
        }
        echo '<h1 style="margin: 10px 0">Database</h1>';
        echo '<pre>';
            echo 'Executed ' . $queryCount . ' queries in ' . sprintf('%.6f', $totalTime) . ' seconds' . "<br />";
            echo '<br />Queries percent: ' . round(($totalTime / $total) * 100, 2) . ' %<br/>';
            echo '<br />Queries:<br />';
            echo implode('', $queries);
            echo 'Average query length: ' . sprintf('%.6f', $totalTime / $queryCount) . ' seconds' . "<br />";
            echo 'Queries per second: ' . sprintf('%.6f', $queryCount / $totalTime) . " seconds<br />";
            echo 'Longest query length: ' . sprintf('%.6f', $longestTime) . " seconds<br />";
            echo "Longest query:<br />";
            echo '<div style="border: 1px Lightid #f2f2f2; padding: 10px; margin: 10px 0">' . $longestQuery . '</div>';
        echo '</pre>';
    }
}

echo '<h1 style="margin: 10px 0">Memory</h1>';
echo '<div>';
    echo 'Usage: ' . number_format((memory_get_usage() / 1024 / 1024), 6) . ' MB';
    echo '<br />Peak usage: ' . number_format((memory_get_peak_usage() / 1024 / 1024), 6) . ' MB';
echo '</div>';

echo '<h1 style="margin: 10px 0">Session</h1>';
echo '<pre>';
    print_r($_SESSION);
echo '</pre>';

echo '<h1 style="margin: 10px 0">Config</h1>';
echo '<pre>';
    print_r(Light_Core::$config);
echo '</pre>';

echo '<h1 style="margin: 10px 0">Modules</h1>';
echo '<pre>';
    print_r(Light_Core::$modules);
echo '</pre>';

echo '<h1 style="margin: 10px 0">Classes</h1>';
echo '<pre>';
    print_r(Light_Loader::$files);
echo '</pre>';

echo '</div>';